#  pip install paho-mqtt==1.6.1
import paho.mqtt.client as mqtt

# MQTT 服务器设置
broker_address = "192.168.3.200"  # 替换为你的 MQTT 服务器地址
port = 1883  # MQTT 默认端口
username = "gggg"  # 替换为你的账号
password = "gggg"  # 替换为你的密码
client_id = "G"  # 替换为你的客户端 ID
topic_to_subscribe = "gggg/#"  # 替换为你想要订阅的主题


def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe("test/topic")


def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))


client = mqtt.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.on_message = on_message

client.connect(broker_address, port=port, keepalive=60)
client.loop_forever()
